# === load relevant libraries
library(data.table)
library(ggplot2)
library(DescTools)
library(plm)
library(lfe)
library(dplyr)
library(sandwich)

rm(list = ls())

# data to be plotted
data = readRDS('input_data/event_study_alternative_hypothesis.RDS')
data = data[rank %in% c(1,2,8,9)]
data[, lab := ifelse(rank == 9, 'Most negatively affected',ifelse(rank == 8, 'Second neg',ifelse(rank == 1, 'Most positively affected', 'Second pos')))]
data[, date := as.Date(as.character(100*yyyymm+1), '%Y%m%d')]
data_bk = copy(data)
event_date = as.Date('2002-06-01')

# Figure 8(a): return on assets
data = copy(data_bk[var == 'roa'])
ggplot(data, aes(x = date, y = value, color = reorder(lab, -rank))) + geom_line(lwd = 1) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.2, .8)) + coord_cartesian(ylim = c(-.01, .02)) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + geom_vline(xintercept = event_date, lty = 3) + 
  labs(x = NULL, y = 'Return on assets')

# Figure 8(b): return on equity
data = copy(data_bk[var == 'roe'])
ggplot(data, aes(x = date, y = value, color = reorder(lab, -rank))) + geom_line(lwd = 1) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.2, .8)) + coord_cartesian(ylim = c(-.02, .05)) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + geom_vline(xintercept = event_date, lty = 3) + 
  labs(x = NULL, y = 'Return on equity')

# Figure 8(c): Trading: Investment companies and advisors
data = copy(data_bk[var == 'inv'])
ggplot(data, aes(x = date, y = value, color = reorder(lab, -rank))) + geom_line(lwd = 1) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.8, .5)) + coord_cartesian(ylim = c(-.03, .02)) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + geom_vline(xintercept = event_date, lty = 3) + 
  labs(x = NULL, y = 'Cumulative trading')

# Figure 8(d): Trading: Other 13F institutions
data = copy(data_bk[var == 'other'])
ggplot(data, aes(x = date, y = value, color = reorder(lab, -rank))) + geom_line(lwd = 1) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.2, .2)) + coord_cartesian(ylim = c(-.03, .02)) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + geom_vline(xintercept = event_date, lty = 3) + 
  labs(x = NULL, y = 'Cumulative trading')

# Figure 8(e): Trading: Hedge funds
data = copy(data_bk[var == 'hf'])
ggplot(data, aes(x = date, y = value, color = reorder(lab, -rank))) + geom_line(lwd = 1) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.2, .2)) + coord_cartesian(ylim = c(-.03, .02)) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + geom_vline(xintercept = event_date, lty = 3) + 
  labs(x = NULL, y = 'Cumulative trading')

# Figure 8(f): Short interest
data = copy(data_bk[var == 'short_interest'])
ggplot(data, aes(x = date, y = value, color = reorder(lab, -rank))) + geom_line(lwd = 1) + theme_classic() + 
  theme(legend.title = element_blank(), legend.position = c(.2, .8)) + coord_cartesian(ylim = c(0, .1)) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + geom_vline(xintercept = event_date, lty = 3) + 
  labs(x = NULL, y = 'Short interest')

